import {storeBindingsBehavior} from "mobx-miniprogram-bindings";
import mall from "@src/store/mall";
import member from "@src/store/member";
import base from "@src/store/base";
import router from "@src/router";
import {getUserPhone} from "@mall/api/address";

Component({
  behaviors: [storeBindingsBehavior],
  storeBindings: [{
    store: mall,
    fields: ['userLocation']
  }, {
    store: base,
    fields: ['isiPhoneX', 'themeColor', 'themeTitleColor', 'barHeight']
  }],
  properties: {
    delivery: {
      type: Object,
      value: null
    },
    userPhone: {
      type: String,
      value: null,
      observer(userPhone) {
        if(userPhone.length>=11){
          this.setData({btnStatus:false})
        }
      }
    },
    store: {
      type: Object,
      value: null
    },
    arrivalTime: {
      type: String,
    },
    prepareTips: {
      type: String,
    },
    groupInfo:{
      type:Object,
      value:null
    },
    deliveryType:{
      type:String,
      value:"",
    },
  },
  options:{
    addGlobalClass:true
  },
  data: {
    address: null,
    btnStatus:true,
    leaderCollect:false
  },
  methods: {
    handleLeaderCollect(e){
      this.setData({leaderCollect:e.detail})
      this.triggerEvent("handleLeaderCollect",e.detail)
    },
    handlePhoneBtn(e){
      if(e.detail.value.length>=11){
        this.setData({btnStatus:false})
      }else {
        this.setData({btnStatus:true})
      }
    },
    //获取用户手机号
    getPhoneNumber(e) {
      if (!e.detail.iv) {
        wx.showToast({ title: "手机号获取失败，请重试", icon: "none", duration: 1500 });
        return;
      }
      let data = {
        ivStr: e.detail.iv,
        encryptedData: e.detail.encryptedData
      }
      getUserPhone(data).then(res => {
        if (res.code === 0) {
          this.setData({userPhone: res.data});
          this.triggerEvent('phoneNumt',{userPhone: res.data})
        } else {
          wx.showToast({title: '手机号获取失败,请稍后重试', icon: 'none'})
        }
      })
    },
    handleToChoose() {
      let store =this.data.store;
      wx.openLocation({latitude: store.latitude,
        longitude: store.longitude,
        name:store.name,
        address: store.city + store.area + store.address, scale: 18})

    },
    handleArrivalTime() {
      this.triggerEvent("arrivalTime")
    },
    handleReservedPhone(e){
      this.triggerEvent('handleReservedPhone',e.detail)
    },
    handleUserPhone(e) {
        // this.setData({userPhone:""});
        this.triggerEvent('phoneNumt',{userPhone: e.detail.value})
    },
    handleSwitchType(e) {
      router.push(
          {
            'name': 'mall/service-stores',
            params: {deliveryType: 'LOCAL', showTabs: true, activeTab: 'address'},
            events: {
              'changeStore': (data) => {
                // 这里只是修改店铺
                this.triggerEvent("store", data)
              },
              'changeAddress': (data) => {
                console.info("changeAddress", data)
                // 这里切换了配送方式和地址
                this.triggerEvent("address", data.address)
                this.triggerEvent("store", {store: this.data.store, 'deliveryType': 'LOCAL'})
              }
            }
        });
    },
  }
});
